Vuetify is a popular UI framework for Vue apps.
In this article, we’ll look at how to work with the Vuetify framework.
Slide Group Centered Active Item
We can center the selected item on the slid group with the center-active
prop:
<template>
<v-container class="grey lighten-5">
<v-row>
<v-col>
<v-sheet class="mx-auto" elevation="8" max-width="800">
<v-slide-group v-model="model" class="pa-4" center-active show-arrows>
<v-slide-item v-for="n in 15" :key="n" v-slot:default="{ active, toggle }">
<v-card
:color="active ? 'primary' : 'grey lighten-1'"
class="ma-4"
height="200"
width="100"
@click="toggle"
>
<v-row class="fill-height" align="center" justify="center">
<v-scale-transition>
<v-icon
v-if="active"
color="white"
size="48"
v-text="'mdi-close-circle-outline'"
></v-icon>
</v-scale-transition>
</v-row>
</v-card>
</v-slide-item>
</v-slide-group>
</v-sheet>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
name: "HelloWorld",
data: () => ({
model: undefined,
}),
};
</script>
Now when we click on an item, it’ll always be centered.
Windows
The v-window
component lets us transition content from one pane to another.
For example, we can write:
<template>
<v-container class="grey lighten-5">
<v-row>
<v-col>
<v-card flat tile>
<v-window v-model="onboarding" vertical>
<v-window-item v-for="n in length" :key="`card-${n}`">
<v-card color="grey" height="200">
<v-row class="fill-height" align="center" justify="center" tag="v-card-text">
<h1 style="font-size: 5rem;" class="white--text">Slide {{ n }}</h1>
</v-row>
</v-card>
</v-window-item>
</v-window>
<v-card-actions class="justify-space-between">
<v-btn text @click="prev">
<v-icon>mdi-chevron-left</v-icon>
</v-btn>
<v-item-group v-model="onboarding" class="text-center" mandatory>
<v-item v-for="n in length" :key="`btn-${n}`" v-slot:default="{ active, toggle }">
<v-btn :input-value="active" icon @click="toggle">
<v-icon>mdi-record</v-icon>
</v-btn>
</v-item>
</v-item-group>
<v-btn text @click="next">
<v-icon>mdi-chevron-right</v-icon>
</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
name: "HelloWorld",
data: () => ({
length: 6,
onboarding: 0,
}),
methods: {
next() {
this.onboarding =
this.onboarding + 1 === this.length ? 0 : this.onboarding + 1;
},
prev() {
this.onboarding =
this.onboarding - 1 < 0 ? this.length - 1 : this.onboarding - 1;
},
},
};
</script>
We have the v-window
component with the v-model
to get and set the index of the slide we’re on.
v-card
is what we’re scrolling through.
The v-card-actions
component has the navigation controls for moving between slides.
The vertical
prop makes the slides display vertically.
The slides are moved with the next
and prev
methods by changing the onboarding
value, which is bound to the v-model
to change the slide.
Reverse
We can reverse the v-window
component with the reverse
component:
<template>
<v-container class="grey lighten-5">
<v-row>
<v-col>
<v-card flat tile>
<v-window v-model="onboarding" vertical reverse>
<v-window-item v-for="n in length" :key="`card-${n}`">
<v-card color="grey" height="200">
<v-row class="fill-height" align="center" justify="center" tag="v-card-text">
<h1 style="font-size: 5rem;" class="white--text">Slide {{ n }}</h1>
</v-row>
</v-card>
</v-window-item>
</v-window>
<v-card-actions class="justify-space-between">
<v-btn text @click="prev">
<v-icon>mdi-chevron-left</v-icon>
</v-btn>
<v-item-group v-model="onboarding" class="text-center" mandatory>
<v-item v-for="n in length" :key="`btn-${n}`" v-slot:default="{ active, toggle }">
<v-btn :input-value="active" icon @click="toggle">
<v-icon>mdi-record</v-icon>
</v-btn>
</v-item>
</v-item-group>
<v-btn text @click="next">
<v-icon>mdi-chevron-right</v-icon>
</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
name: "HelloWorld",
data: () => ({
length: 6,
onboarding: 0,
}),
methods: {
next() {
this.onboarding =
this.onboarding + 1 === this.length ? 0 : this.onboarding + 1;
},
prev() {
this.onboarding =
this.onboarding - 1 < 0 ? this.length - 1 : this.onboarding - 1;
},
},
};
</script>
Now the slides move in reverse when we click on the navigation controls.